package org.sbbs.app.demo.webapp.action;

import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.sbbs.app.demo.model.DemoEntity;
import org.sbbs.app.demo.service.DemoEntityManager;
import org.sbbs.base.search.PropertySearchBuilder;
import org.sbbs.base.util.excel.FillManager;
import org.sbbs.base.util.excel.Layouter;
import org.sbbs.base.webapp.action.BaseGridAction;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;

public class DemoDwzPagedAction
    extends BaseGridAction {

    public String gridPageList() {
        this.showSrpingBeans();
        HttpServletRequest req = this.getRequest();
        Search search = PropertySearchBuilder.BuildSearch( getRequest() );
        SearchResult srt = this.demoEntityManager.searchAndCount( search );
        this.setPageList( srt.getResult() );
        this.records = srt.getTotalCount();
        this.total = getTotlePage( records, search.getMaxResults() );
        this.page = search.getPage() + 1;
        this.setStatusCode( AJAX_STATUS_SUCCESS );
        this.setMessage( "load success,dwz message test." );
        return this.SUCCESS;
    }

    private DemoEntityManager demoEntityManager;

    public DemoEntityManager getDemoEntityManager() {
        return demoEntityManager;
    }

    public void setDemoEntityManager( DemoEntityManager demoEntityManager ) {
        this.demoEntityManager = demoEntityManager;
    }

 
    public String excelExport() {
        try {
            String excelType = this.getRequest().getParameter( "oper" );
            ;
            String fileName = "DemoEntityExport";

            String sheetName = "sheet";
            int sheetCounter = 1;
            String reportTitles = "样例导出报告";
            String[] labels =
                { getText( "demoEntity.demoId" ), getText( "demoEntity.intField" ), getText( "demoEntity.longField" ),
                    getText( "demoEntity.shortField" ), getText( "demoEntity.floatField" ), getText( "demoEntity.doubleField" ) };
            String[] propertyNames = { "demoId", "intField", "longField", "shortField", "floatField", "doubleField" };

            Search search = PropertySearchBuilder.BuildSearch( getRequest() );
            SearchResult srt = this.demoEntityManager.searchAndCount( search );
            search.setMaxResults( 1000 );
            int totalPage = getTotlePage( srt.getTotalCount(), search.getMaxResults() );

            Workbook workbook = null;
            if ( excelType.equalsIgnoreCase( "xlsx" ) ) {
                workbook = new SXSSFWorkbook( 10000 );

            }
            else {
                workbook = new HSSFWorkbook();
            }
            Sheet worksheet = workbook.createSheet( sheetName + "_" + sheetCounter++ );

            int startRowIndex = 0;
            int startColIndex = 0;
            int offset = startRowIndex + 2;

            Layouter.buildReport( worksheet, startRowIndex, startColIndex, labels, reportTitles );

            for ( int i = 0; i < totalPage; i++ ) {
                search.setPage( i );
                List dsl = this.demoEntityManager.searchAndCount( search ).getResult();
                offset = FillManager.fillReportPaged( worksheet, offset, startColIndex, dsl, DemoEntity.class, propertyNames );

            }


            HttpServletResponse resp = ServletActionContext.getResponse();
            resp.setContentType( "application/vnd.ms-excel" );
            resp.setHeader( "Content-Disposition", "attachment;filename="
                + new String( ( fileName + "." + excelType ).getBytes( "GBK" ), "ISO8859-1" ) );

            OutputStream outs = resp.getOutputStream();
            worksheet.getWorkbook().write( outs );
            outs.flush();
            outs.close();
        }
        catch ( Exception e ) {
            e.printStackTrace();
        }
        return null;
    }

    private void showSrpingBeans() {
        WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext( this.getRequest().getSession().getServletContext() );
        String[] ns = wac.getBeanDefinitionNames();
        for ( int i = 0; i < ns.length; i++ ) {
           // this.LOG.info( ns[i] );

        }
    }
}
